package com.example.Sao.Adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: scan
 * @Package: com.example.MyApplication.Adapter
 * @ClassName: MyFragmentPagerAdapter
 * @Description: FragmentPager管理适配器
 * @Author: 阿毛
 * @CreateDate: 2020/2/21 19:08
 * @UpdateUser: 更新者：
 * @UpdateDate: 2020/2/21 19:08
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
    private List<Fragment> fragmentList = new ArrayList<>();
    public MyFragmentPagerAdapter(FragmentManager fr){
        super(fr);
    }
    public MyFragmentPagerAdapter(FragmentManager fr,List<Fragment>list){
        super(fr);
        this.fragmentList = list;
    }
    @Override
    public Fragment getItem(int position) {
        return fragmentList.get(position);
    }

    @Override
    public int getCount() {
        return fragmentList.size();
    }

    public void addFragment(Fragment fr){
        fragmentList.add(fr);
    }

    public void setFragments(List<Fragment> newFragmentList,FragmentManager fm) {
        if (this.fragmentList != null) {
            FragmentTransaction ft = fm.beginTransaction();//获得FragmentTransaction 事务
            for (Fragment f : this.fragmentList) {
                ft.remove(f); //遍历删除fragment
            }
            ft.commit();
            ft = null;
            fm.executePendingTransactions();
        }
        this.fragmentList = newFragmentList;//重新赋值
        notifyDataSetChanged();
    }
}

